using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace _3D_Game
{
    
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;

        public Camera camera { get; protected set; }
        ModelManager modelManager;

        public Random rnd { get; protected set; }

        float shotSpeed = 10;
        int shotDelay = 300;
        int shotCountdown = 0;
        Texture2D crosshairTexture;
        SpriteFont fontScore;
        int score;
        int gameLevel;
        Vector2 scorePosition = new Vector2(30, 50);
        Vector2 gameLevelPosition = new Vector2(30, 30);
        List<BasicModel> models = new List<BasicModel>();
        List<BasicModel> shots = new List<BasicModel>();

        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
            //instantiate model manager and connect it to game loop
            modelManager = new ModelManager(this);
            Components.Add(modelManager);
            rnd = new Random();

        }

        protected override void Initialize()
        {
            camera = new Camera(this, new Vector3(0, 0, 50),
                Vector3.Zero, Vector3.Up);
            Components.Add(camera);

            base.Initialize();
        }

        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);

            // To load the game content
            crosshairTexture = Content.Load<Texture2D>(@"Texture\Crosshair");
            fontScore = Content.Load<SpriteFont>("Fonts/Lucida Console");
           // bulletModel = Content.Load<Model>("Models/pea_proj");
        }


        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        protected override void Update(GameTime gameTime)
        {
            // See if the player has fired a shot
            FireShots(gameTime);

            base.Update(gameTime);
        }

        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);

            // draw the crosshair on the screen
            spriteBatch.Begin();
            spriteBatch.Draw(crosshairTexture,
            new Vector2((Window.ClientBounds.Width / 2)
            - (crosshairTexture.Width / 2),
            (Window.ClientBounds.Height / 2)
            - (crosshairTexture.Height / 2)),
            Color.White);
            spriteBatch.End();
            // display the game levels and scores on the screen
            spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend);
            spriteBatch.DrawString(fontScore, "Score : " + score, scorePosition, Color.LightGreen);
            spriteBatch.DrawString(fontScore, "Level : " + gameLevel, gameLevelPosition, Color.LightGreen);
            spriteBatch.End();


            base.Draw(gameTime);
        }

        protected void FireShots(GameTime gameTime)
        {
           
               if (shotCountdown <= 0)
            {
                 // Did player press space bar or left mouse button?
                if (Keyboard.GetState( ).IsKeyDown(Keys.Space) ||
                    Mouse.GetState( ).LeftButton == ButtonState.Pressed)
                {
                    // Add a shot to the model manager
                    modelManager.AddShot(camera.cameraPosition + new Vector3(0, -5, 0),
                    camera.GetCameraDirection * shotSpeed);
                    // Reset the shot countdown
                    shotCountdown = shotDelay; 
                }
             }
             else
                shotCountdown -= gameTime.ElapsedGameTime.Milliseconds;
          
        }
        
        public void AddScore()
        {
            score += ParticleSettings.scoreEarn;
        }
        public void AddLevel()
        {
            gameLevel = score / 50;
            
        }

    }
}
